<?php
namespace Console;
use Swiftx\Http\Post;
use Swiftx\Http\Session;

/**
+---------------------------------------------------------------------------------------------------------------
 * 控制台控制器类
+---------------------------------------------------------------------------------------------------------------
 * @author		胡永强  <odaytudio@gmailcom>
 * @since		2015-02-25
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
+---------------------------------------------------------------------------------------------------------------
 */
class Admin {

    /**
     * 控制台主面板
     */
    public static function PageLayout(){
        $view = $GLOBALS->Smarty;
        $view->assign('title', '房阿玛房产交易系统');
        $view->assign('menu', include 'Config'.DS.'Menu.int.php');
        $view->display('Console'.DS.'Index.tpl');
    }

    /**
     * 登陆页面
     */
    public static function PageLogin(){
        $view = $GLOBALS->Smarty;
        $view->assign('title', '平台管理中心');
        $view->display('Console'.DS.'Login.tpl');
    }

    /**
     * 登陆处理
     */
    public static function ActionLogin(){
        // 校验图片验证码
        if(Common::CheckVerify(Post::Value('captcha'))){
            die(json_encode(array('code'=>503)));
        }
        // 用户账号存在
        else if($admin = $GLOBALS->DB->Table('es_adminuser')->Where('username',Post::Value('username'))->Row){
            // 用户状态异常
            if($admin['state'] != 1){
                die(json_encode(array('code'=>500)));
            }
            // 用户密码正确
            else if($admin['password'] == Post::Value('password')){
                Session::Set('Admin', $admin['userid']);
                die(json_encode(array('code'=>200)));
            }
            // 用户密码不正确
            die(json_encode(array('code'=>502)));
        }
        // 用户账号不存在
        else {
            die(json_encode(array('code'=>501)));
        }
    }

    /**
     * 管理员是否登陆
     * @return \Swiftx\Http\boolen
     */
    public static function IsLogin(){
        return Session::Exists('Admin');
    }

    /**
     * 主控制台
     */
    public static function PageBashboard(){
        $view = $GLOBALS->Smarty;
        $view->display('Admin'.DS.'Bashboard.tpl');
    }
}